﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccessLayer;
using BusinessLogicLayer;


namespace ChamCong.LanhDao
{
    public partial class QuanLyBaoCaoGiaoViec : BasePage
    {
        GiaoViecBLL gvBLL = new GiaoViecBLL();
        CT_GiaoViecBLL ctgvBLL = new CT_GiaoViecBLL();
        NhanVienBLL nvBLL = new NhanVienBLL();
        PhanQuyenBLL pq = new PhanQuyenBLL();
        QuanLyPhanQuyenBLL q = new QuanLyPhanQuyenBLL();
        TinhHieuSuatCV tinhHS = new TinhHieuSuatCV();

        public static int _magv;
        public static int _MaPB;
        public static int _MaNV;
        int _ttd;
        protected void Page_Load(object sender, EventArgs e)
        {
            _ttd = Convert.ToInt32(Request.QueryString["ttd"]);
            try
            {
                if (_ttd != 0)
                {
                }
                else
                {
                    _ttd = 3;
                }
                if (!IsPostBack)
                {
                    //Session["MaNV"] = 1001;
                    _MaNV = Convert.ToInt32(Session["MaNV"]);
                    _MaPB = nvBLL.TimPhongBan(_MaNV);
                    bindGridView(0, _MaPB, _ttd);
                }
            }
            catch (Exception ex)
            {
                sbcMsgBox1.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Error, "Thông báo", ex.Message);
            }            
        }

        public void bindGridView(int _manv, int _mapb, int _tt)
        {
            tbChiTiet.Visible = false;
            if (gvBLL.LayDanhSachGiaoViec(0, _mapb, _tt).Count > 0)
            {
                gvGiaoViec.Visible = true;
                gvGiaoViec.DataSource = gvBLL.LayDanhSachGiaoViec(0, _mapb, _tt);
                gvGiaoViec.DataBind();

            }
            else
            {
                sbcMsgBox1.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Warning, "Thông báo", "Hiện thời không có dữ liệu!");
            }

        }


        protected void gvGiaoViec_rdb(object sender, GridViewRowEventArgs e)
        {
            GridViewRow gvr = (GridViewRow)e.Row;
            string rowID = String.Empty;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                LinkButton _linkbutton = (LinkButton)gvr.FindControl("likbtnChiTiet");
                e.Row.Attributes.Add("onclick", "ChangeRowColor(this)");

                e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.backgroundColor='#EEFF00'";
                e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';this.style.backgroundColor='White'";
                //e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.gvGiaoViec_DaDuyet, "Select$" + e.Row.RowIndex);

                string ma = DataBinder.Eval(e.Row.DataItem, "MaGiaoViec").ToString();
                _linkbutton.Attributes.Add("MaGiaoViec", ma);
            }
        }

        protected void likbtnChiTiet_Click(object sender, EventArgs e)
        {
            LinkButton likbtn = (LinkButton)sender;
            _magv = int.Parse(likbtn.Attributes["MaGiaoViec"]);

            if (gvBLL.LayGiaoViec(_magv) != null)
            {
                GIAOVIEC a = gvBLL.LayGiaoViec(_magv);
                lbHoTen.Text = a.NHANVIEN.HoTen.ToString();
                lbMaNV.Text = a.MaNV.ToString();
                lbTuNgay.Text = String.Format("{0:dd/MM/yyyy}", a.TuNgay);
                lbDenNgay.Text = String.Format("{0:dd/MM/yyyy}", a.DenNgay);
                txtChuThich.Text = a.ChuThich;

               
                tbChiTiet.Visible = true;
                if (_ttd == 3)
                {
                    txtChuThich.Enabled = true;
                    btnDuyet.Visible = true;
                    btnKhongDuyet.Visible = true;
                }
                else
                {
                    txtChuThich.Enabled = false;
                    btnDuyet.Visible = false;
                    btnKhongDuyet.Visible = false;
                }
                

                //Lay Danh Sách chi tiết giao việc
                gvKQCongViec.DataSource = ctgvBLL.LayDSCTGiaoViec2(_magv);
                gvKQCongViec.DataBind();
            }
            else { }
        }


        protected void btnDuyet_Click(object sender, EventArgs e)
        {
            try
            {
                GIAOVIEC a = new GIAOVIEC();
                a.MaGiaoViec = _magv;
                a.ChuThich = txtChuThich.Text;
                a.TrangThai = 4;
                a.KetQua = tinhHS.TinhHieuSuatTuan(_magv);
                a.TrangThaiDoc = 0;

                gvBLL.Sua(a);

                bindGridView(0, _MaPB, _ttd);

                tbChiTiet.Visible = false;

                sbcMsgBox3.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Success, "Thông báo", "Báo cáo đã được duyệt!");
            }
            catch { }
        }

        protected void btnKhongDuyet_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtChuThich.Text == "")
                {
                    sbcMsgBox2.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Warning, "Thông báo", "Phải chú thích khi không duyệt!");
                }
                else
                {
                    GIAOVIEC a = new GIAOVIEC();
                    a.MaGiaoViec = _magv;
                    a.ChuThich = txtChuThich.Text;
                    a.TrangThai = 5;
                    a.TrangThaiDoc = 0;
                    gvBLL.Sua(a);

                    bindGridView(0, _MaPB, _ttd);


                    tbChiTiet.Visible = false;
                    sbcMsgBox3.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Success, "Thông báo", "Báo cáo không được duyệt!");
                }
            }
            catch { }
        }

        protected void gvKQCongViec_rdb(object sender, GridViewRowEventArgs e)
        {
            GridViewRow gvr = (GridViewRow)e.Row;

            if (DataBinder.Eval(e.Row.DataItem, "KetQua") == null)
            { }
            else
            {
                float chitieu = float.Parse(DataBinder.Eval(e.Row.DataItem, "ChiTieu").ToString());
                float ketqua = float.Parse(DataBinder.Eval(e.Row.DataItem, "KetQua").ToString());

                if (ketqua > (chitieu + (chitieu * 0.2)))
                {
                    gvr.Cells[5].BackColor = System.Drawing.Color.PowderBlue;
                }
                else if (ketqua < (chitieu - (chitieu * 0.2)))
                {
                    //gvr.Cells[5].ForeColor = System.Drawing.Color.Red;
                    gvr.Cells[5].BackColor = System.Drawing.Color.LightPink;
                }
                else
                {
                    gvr.Cells[5].BackColor = System.Drawing.Color.PaleGreen;
                }
            }
        }

        protected string AutoConvert(object ketqua)
        {
            if (ketqua == null)
            {
                return "";
            }
            else
            {
                return (float.Parse(ketqua.ToString())).ToString();
            }
        }

        protected void gvGiaoViec_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvGiaoViec.PageIndex = e.NewPageIndex;
            this.bindGridView(0, _MaPB, _ttd);
        }

    }
}